"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fileDB = exports.FileDB = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
class FileDB {
    constructor() {
        this.dbFile = path_1.default.join(__dirname, 'uploads.json');
        this.data = this.initializeDB();
    }
    initializeDB() {
        if (!fs_1.default.existsSync(this.dbFile)) {
            const initialData = { uploads: [] };
            fs_1.default.writeFileSync(this.dbFile, JSON.stringify(initialData, null, 2));
            return initialData;
        }
        const data = fs_1.default.readFileSync(this.dbFile, 'utf-8');
        return JSON.parse(data);
    }
    saveDB() {
        fs_1.default.writeFileSync(this.dbFile, JSON.stringify(this.data, null, 2));
    }
    // 添加新的上传记录
    addUpload(filename, filePath) {
        const record = {
            filename,
            path: filePath,
            uploadTime: new Date().toISOString()
        };
        this.data.uploads.push(record);
        this.saveDB();
        return record;
    }
    // 获取所有上传记录
    getAllUploads() {
        return this.data.uploads;
    }
    // 根据文件名查找记录
    findByFilename(filename) {
        return this.data.uploads.find(record => record.filename === filename);
    }
    // 获取最新的上传记录
    getLatestUpload() {
        return this.data.uploads[this.data.uploads.length - 1];
    }
}
exports.FileDB = FileDB;
// 导出单例实例
exports.fileDB = new FileDB();
